﻿using Microsoft.Practices.Unity;
using StockholmsStad.Runtime.Serialization.DataContract;
using StockholmsStad.Windows.UI.Infrastructure.Interfaces;
using StockholmsStad.Windows.UI.Modules.Maintenance.Interfaces;
using StockholmsStad.Windows.UI.Facade;
using StockholmsStad.Runtime.Serialization.DataContract.Enums;
using System.Collections.ObjectModel;
using StockholmsStad.Windows.UI.Modules.Maintenance.Models;
using Microsoft.Practices.Composite.Events;

namespace StockholmsStad.Windows.UI.Modules.Maintenance.Services
{
    public class TemplatesService : ITempatesService
    {
        private IApplicationService applicationService;
        [Dependency]
        public IApplicationService ApplicationService
        {
            set { applicationService = value; }
        }

        #region ITempatesService Members

        public TemplatesPresentationModel AddNewMarriageCertificate()
        {
            var model = new TemplatesPresentationModel();
            model.Added += model_Added;
            model.Saved += model_Saved;
            return model;
        }

        public ObservableCollection<ValueIdPair> GetAvailableLanguagesForReportDefinition(MarriageCertificateType marriageCertificateType)
        {
            var user = applicationService.GetCurrentUser();
            var facade = new MaintenanceFacade(user);
            var types = facade.GetAvailableLanguagesForReportDefinition(marriageCertificateType);
            var list = new ObservableCollection<ValueIdPair>(types);
            return list;
        }

        public ObservableCollection<MarriageCertificateType> GetMarriageCertificateTypes()
        {
            var user = applicationService.GetCurrentUser();
            var facade = new MaintenanceFacade(user);
            var types = facade.GetMarriageCertificateTypes();
            var list = new ObservableCollection<MarriageCertificateType>(types);
            return list;
        }

        public ObservableCollection<ValueIdPair> GetLanguagesForCertificateType(MarriageCertificateType marriageCertificateType)
        {
            var user = applicationService.GetCurrentUser();
            var facade = new MaintenanceFacade(user);
            var langs = facade.GetLanguagesForCertificateType(marriageCertificateType);
            var list = new ObservableCollection<ValueIdPair>(langs);
            return list;
        }

        public TemplatesPresentationModel GetMarriageCertificate(MarriageCertificateType marriageCertificateType, ValueIdPair language)
        {
            var user = applicationService.GetCurrentUser();
            var facade = new MaintenanceFacade(user);
            var certificate = facade.GetMarriageCertificate(marriageCertificateType, language.Id);
            var model = CreateTemplatesModel(certificate);
            model.SelectedLanguage = language;
            model.SelectedMarriageCertificateType = marriageCertificateType;
            return model;
        }

        private static MarriageCertificate CreateMarriageCertificate(TemplatesPresentationModel model)
        {
            var certificate = new MarriageCertificate
                                  {
                                      Language = model.SelectedLanguage,
                                      MarriageCertificateType = model.SelectedMarriageCertificateType,
                                      ReportDefinition = new ReportDefinition
                                                             {
                                                                 AboveDate = model.AboveDate,
                                                                 BelowDate = model.BelowDate,
                                                                 BelowSignature = model.BelowSignature,
                                                                 BetweenNames = model.BetweenNames,
                                                                 City = model.City,
                                                                 Heading = model.Heading,
                                                                 Inception = model.Inception,
                                                                 SignLine = model.SignLine,
                                                             }
                                  };

            if (model.ReportDefinitionId.HasValue)
                certificate.ReportDefinition.ReportDefinitionId = model.ReportDefinitionId.Value;

            if (model.MarriageCertificateId.HasValue)
                certificate.MarriageCertificateId = model.MarriageCertificateId;

            return certificate;
        }

        private TemplatesPresentationModel CreateTemplatesModel(MarriageCertificate certificate)
        {
            var model = new TemplatesPresentationModel();

            if (certificate.MarriageCertificateId.HasValue)
                model.MarriageCertificateId = certificate.MarriageCertificateId.Value;

            if (certificate.ReportDefinition != null)
            {
                if (certificate.ReportDefinition.ReportDefinitionId.HasValue)
                    model.ReportDefinitionId = certificate.ReportDefinition.ReportDefinitionId.Value;

                if (!string.IsNullOrEmpty(certificate.ReportDefinition.AboveDate))
                    model.AboveDate = certificate.ReportDefinition.AboveDate;

                if (!string.IsNullOrEmpty(certificate.ReportDefinition.BelowDate))
                    model.BelowDate = certificate.ReportDefinition.BelowDate;

                if (!string.IsNullOrEmpty(certificate.ReportDefinition.BelowSignature))
                    model.BelowSignature = certificate.ReportDefinition.BelowSignature;

                if (!string.IsNullOrEmpty(certificate.ReportDefinition.BetweenNames))
                    model.BetweenNames = certificate.ReportDefinition.BetweenNames;

                if (!string.IsNullOrEmpty(certificate.ReportDefinition.City))
                    model.City = certificate.ReportDefinition.City;

                if (!string.IsNullOrEmpty(certificate.ReportDefinition.Heading))
                    model.Heading = certificate.ReportDefinition.Heading;

                if (!string.IsNullOrEmpty(certificate.ReportDefinition.Inception))
                    model.Inception = certificate.ReportDefinition.Inception;

                if (!string.IsNullOrEmpty(certificate.ReportDefinition.SignLine))
                    model.SignLine = certificate.ReportDefinition.SignLine;
            }

            model.Saved += model_Saved;
            model.Deleted += model_Deleted;
            model.Added += model_Added;
            return model;
        }

        #region events
        
        private void model_Added(object sender, DataEventArgs<TemplatesPresentationModel> e)
        {
            if (e != null && e.Value != null)
            {
                var model = e.Value;
                var user = applicationService.GetCurrentUser();
                var facade = new MaintenanceFacade(user);
                var cert = CreateMarriageCertificate(model);
                int id = facade.AddMarriageCertificate(cert);
                model.MarriageCertificateId = id;
            }
        }

        private void model_Saved(object sender, DataEventArgs<TemplatesPresentationModel> e)
        {
            if (e != null && e.Value != null)
            {
                var model = e.Value;

                if (model.ShouldSave)
                {
                    var user = applicationService.GetCurrentUser();
                    var facade = new MaintenanceFacade(user);

                    var cert = CreateMarriageCertificate(model);

                    if (cert.MarriageCertificateId.HasValue)
                        facade.UpdateMarriageCertificate(cert);
                    else
                    {
                        int id = facade.AddMarriageCertificate(cert);
                        model.MarriageCertificateId = id;
                    }
                }
            }
        }

        private void model_Deleted(object sender, DataEventArgs<TemplatesPresentationModel> e)
        {
            if (e != null && e.Value != null)
            {
                var model = e.Value;

                var user = applicationService.GetCurrentUser();
                var facade = new MaintenanceFacade(user);

                var cert = CreateMarriageCertificate(model);
                facade.DeleteMarriageCertificate(cert);
            }
        }
        #endregion

        #endregion
    }
}